﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Controls;

namespace Presentation.Views.Converters
{
    /// <summary>
    /// Calculates the column width required to fill the view in a GridView
    /// For usage examples, see http://leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/
    /// </summary>
    public class GridViewWidthConverter : IValueConverter
    {
        /// <summary>
        /// Converts the specified value.
        /// </summary>
        /// <param name="value">The parent Listview.</param>
        /// <param name="type">The type.</param>
        /// <param name="parameter">
        /// If no parameter is given, the remaning width will be returned.
        /// If the parameter is an integer acts as MinimumWidth, the remaining with will be returned only if it's greater than the parameter
        /// If the parameter is anything else, it's taken to be a percentage. Eg: 0.3* = 30%, 0.15* = 15%
        /// </param>
        /// <param name="culture">The culture.</param>
        /// <returns>The width, as calculated by the parameter given</returns>
        public object Convert(object value, Type type, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            string parm = (string)parameter;

            if (parm == null)
            {
                parm = "";
            }

            if (parm[0] == 'P' || parm[0] == 'p')
            {
                ListView listView = value as ListView;

                double percentage = double.Parse(parm.Substring(1));
                return listView.ActualWidth * percentage;
            }
            else if (parm[0] == 'C' || parm[0] == 'c')
            {
                ListView listView = value as ListView;
                GridView grdView = listView.View as GridView;

                double total = 0;
                for (int i = 0; i < grdView.Columns.Count; i++)
                {
                    if (i != int.Parse(parm.Substring(1)))
                    {
                        total += grdView.Columns[i].ActualWidth;
                    }
                }
                double remainingWidth = listView.ActualWidth - total;

                return remainingWidth;
            }
            else if (parm[0] == 'M' || parm[0] == 'm')
            {
                GridViewColumn column = value as GridViewColumn;
                int minWidth = int.Parse(parm.Substring(1));

                if (column.ActualWidth < minWidth)
                {
                    return minWidth;
                }
                else
                {
                    return column.Width;
                }
            }
            else
            {
                return 100;
            }
        }

        public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
